/**	
 * Author : Mht
 * Desc : For 2016 RDM 
 */
package bean;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import lombok.extern.log4j.Log4j;
import redis.clients.jedis.Jedis;
import service.RedisPushService;
import service.RedisService;

/**
 * @author Mht
 * @date May 10, 2016
 */
@Log4j
@RestController
public class RedisController extends BaseController {

	@Autowired
	private RedisService redisService;

	@Autowired
	private RedisPushService redisPushService;

	private Jedis jedis;

	@RequestMapping("/queryRootPath")
	public String queryRootPath(HttpServletRequest request) {
		return queryPath(request);
	}

	@RequestMapping("/execAddUser")
	@ResponseBody
	public void execAddUser() {

		try {
			jedis = redisService.getJedis();
			Map<String, String> map = new HashMap<String, String>();
			long startTime = System.currentTimeMillis();
			for (int i = 0; i < 10000; i++) {
				map.put("name", "Mu Haitao");
				map.put("age", "27");
				map.put("Date", new Date().toString());
				jedis.hmset("user" + i, map);
				map.clear();
			}
			List<String> redisMap = jedis.hmget("user", "name", "age", "Date");
			for (int i = 0; i < 10000; i++) {
				redisMap = jedis.hmget("user" + i, "name", "age", "Date");
				System.out.println("redisMap" + i + " : " + redisMap);
			}
			long endTime = System.currentTimeMillis();
			log.info("Lost Time : " + (endTime - startTime) / 1000 + "s");
		} catch (Exception e) {
			log.error("Add Error : " + e.getMessage());
		} finally {
			redisService.returnResource(jedis);
		}
	}

	@RequestMapping("/execSpringRedis")
	@ResponseBody
	public void execSpringRedis() {
		redisPushService.addUser();
		redisPushService.insert();
	}
}
